package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang 复制追加确认保存
 */
public class CarAddResultController extends TmsController {

	private Logger logger = Logger.getLogger(CarAddResultController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			Sequence seq = Sequence.getInstance();
			CarCreationInputViewHelper viewHelper = (CarCreationInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			car = setEntityFromViewHelper(viewHelper, car);

			TmsUserProfile profile = uc.getUserProfile();
			car.setUpdateUserId(profile.getUniqueId());// 存入更新者Id
			// 上传图片部分，判断某部位是否有图片 图片保存在图片表中
			// List<CarImage> list = (List<CarImage>)uc.getAttribute("imgList");
			List<CarImage> list = new ArrayList<CarImage>();
			if (!"".equals(viewHelper.getImg1())
					&& viewHelper.getImg1() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("1");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg1());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg2())
					&& viewHelper.getImg2() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("2");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg2());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg3())
					&& viewHelper.getImg3() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("3");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg3());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg4())
					&& viewHelper.getImg4() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("4");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg4());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg5())
					&& viewHelper.getImg5() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("5");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg5());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg6())
					&& viewHelper.getImg6() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("6");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg6());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg7())
					&& viewHelper.getImg7() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("7");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg7());
				list.add(entity);
			}
			if (!"".equals(viewHelper.getImg8())
					&& viewHelper.getImg8() != null) {
				CarImage entity = CarImage.getInstance();
				entity.setImgKey("8");
				entity.setImgType("1");
				entity.setImgPath(viewHelper.getImg8());
				list.add(entity);
			}
			// 如果是待入库的车辆，要取得对应的id值
			if (uc.getAttribute("id") != null) {
				String id = (String) uc.getAttribute("id");
				// 新录入车辆是在待入库部分（需要给录入完的车辆的状态进行修改）
				car.setInnerType(2);
				car.setId(Long.parseLong(id));
			}
			String sysPath = (String) uc.getAttribute("sysPath");

			// 车辆信息保存
			car.insertCar(car, list, sysPath, seq.getSerial(1),1);
			uc.removeAttribute("imgList");
			controller.commit();
			logger.debug(this.getClass().getName() + "#execute was completed.");
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 把viewHelper的值赋给Entity
	 * 
	 * @param viewHelper
	 * @param car
	 * @return
	 * @throws TmsDaoException
	 */
	private Car setEntityFromViewHelper(CarCreationInputViewHelper viewHelper,
			Car car) throws TmsDaoException {
		Codes code = Codes.getInstance();

		car.setAttribute(code.getItemCodeByItemIdAndItemValue(
				Codes.ATTACHMENT_PROPERTY, viewHelper.getAttribute()));
		car.setBrandId(viewHelper.getBrandId());
		car.setCardType(code.getItemCodeByItemIdAndItemValue(Codes.PLATE_TYPE,
				viewHelper.getCardType()));
		car.setLicensePlate(viewHelper.getLicensePlate());
		car.setCarOwner(viewHelper.getCarOwner());
		car.setCarOwnerIdCardNo(viewHelper.getCarOwnerIdCardNo());
		car.setCarType(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_TYPE,
				viewHelper.getCarType()));
		car.setCarUse(code.getItemCodeByItemIdAndItemValue(Codes.USER_NATURE,
				viewHelper.getCarUse()));
		car.setColor(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COLOR,
				viewHelper.getColor()));
		car.setCompanyContactPerson(viewHelper.getCompanyContactPerson());
		car.setCompanyContactPhone(viewHelper.getCompanyContactPhone());
		car.setCountryStyle(code.getItemCodeByItemIdAndItemValue(
				Codes.VEHICLE_COUNTRY, viewHelper.getCountryStyle()));
		if ("".equals(viewHelper.getDisplacement())) {
			car.setDisplacement(0);
		} else {
			car.setDisplacement(Integer.parseInt(viewHelper.getDisplacement()));
		}

		car.setEngine(viewHelper.getEngine());
		car.setFuelType(code.getItemCodeByItemIdAndItemValue(Codes.FUEL_TYPE,
				viewHelper.getFuelType()));
		car.setGearbox(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,
				viewHelper.getGearbox()));
		car.setJqx(viewHelper.getJqx());
		if ("".equals(viewHelper.getLoadquality())) {
			car.setLoadquality(0);
		} else {
			car.setLoadquality(Integer.parseInt(viewHelper.getLoadquality()));
		}

		car.setCarStyleId(viewHelper.getCarStyleId());
		//
		Member member = Member.getInstance();
		car.setMemberId(member.getMemIdByMemName(viewHelper.getMemberId()));
		if ("".equals(viewHelper.getMileage())) {
			car.setMileage(0);
		} else {
			car.setMileage(Integer.parseInt(StringUtil.removeDH(viewHelper.getMileage())));
		}

		car.setModelCode(viewHelper.getModelCode());
		car.setOwnerAddress(viewHelper.getOwnerAddress());
		car.setOwnerCardNo(viewHelper.getOwnerCardNo());
		car.setOwnerOrgNo(viewHelper.getOwnerOrgNo());
		car.setOwnerPhone(viewHelper.getOwnerPhone());
		car.setOwnerType(code.getItemCodeByItemIdAndItemValue(Codes.CAR_OWNER,
				viewHelper.getOwnerType()));
		if ("".equals(viewHelper.getPassenger())) {
			car.setPassenger(0);
		} else {
			car.setPassenger(Integer.parseInt(viewHelper.getPassenger()));
		}
		if ("".equals(viewHelper.getPrice())) {
			car.setPrice(0);
		} else {
			car.setPrice(Integer.parseInt(StringUtil.removeDH(viewHelper.getPrice())));
		}
		car.setCarName(viewHelper.getCarName());
		car.setVendorId(viewHelper.getVendorId());
		car.setRegDate(viewHelper.getRegDate());
		car.setRegisterCardNo(viewHelper.getRegisterCardNo());
		car.setSendCardDate(viewHelper.getSendCardDate());
		car.setSerial(viewHelper.getSerial());
		car.setUpdateTime(viewHelper.getUpdateTime());
		car.setUpdateUserId(viewHelper.getUpdateUserId());
		car.setVendorCode(viewHelper.getVendorCode());
		car.setVin(viewHelper.getVin());
		car.setYearCarefulPeriod(viewHelper.getYearCarefulPeriod());
		return car;
	}
}
